package com.tuo51.evaluate.domain.appusers;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.tuo51.foudation.constant.APPConstants;
import com.tuo51.foudation.model.Domain;
import lombok.Data;

import javax.persistence.*;

/**
 * 优惠码
 */

@Data
@Entity
@Table(name = "promo_code")
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
public class PromoCode extends Domain {

	@Column(name = "type", columnDefinition = "int deafault 0 COMMENT '折扣类型，0.按百分数，1.按总额' ")
	private Integer type;

	@Column(name = "rebate", columnDefinition = "int deafault 100 COMMENT '抵扣价格，百分数' ")
	private Integer rebate;

	@Column(name = "code", columnDefinition = "varchar(10) COMMENT '抵扣码' ", unique = true, nullable = false)
	private String code;

	@Column(name = "product_id")
	private Long productId;  // always leads to vip id

	@JsonIgnore
	@OneToOne
	@JoinColumn(name = "product_id", insertable = false, updatable = false)
	private Vip product;

	@Column(name = "comment")
    private String comment;

	@Column(name = "dealer")
    private String dealer;

	@Column(name = "dealer_phone")
	private String dealerPhone;

	//总次数， 可用次数等于 总数量 - 销售数量
	@Column(name = "sales_quota", columnDefinition = "int default 0 COMMENT '销售配额' ")
	private Integer salesQuota;

	//销售数量
    @Column(name = "sales_quantity", columnDefinition = "int default 0 COMMENT '销售数量' ")
    private Integer salesQuantity;

    //销售总额
	@Column(name = "sales", columnDefinition = "int default 0 COMMENT '销售总额' ")
	private Integer sales;

	public Integer calc(Integer price){
		Integer newPrice = price;

		if (getType().intValue() == APPConstants.PromoCodeType.Percent.getValue()) {
			newPrice = price * getRebate() / 100 / 100;  //去掉个位和十位
			newPrice *= 100;
		}else if (getType().intValue() == APPConstants.PromoCodeType.Total.getValue()){
			newPrice = price - getRebate();  //去掉个位和十位
		}

		if(newPrice <= 0){
			newPrice = 1;
		}

		return newPrice;
	}

}
